<template>
  <!-- 注册页面的整体盒子 -->
  <div class="register-container">
    <!-- 注册的盒子 -->
    <div class="register-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <hr />
      <el-form
        style="margin-top: 30px"
        :model="loginFrom"
        :rules="loginFormRules"
        ref="loginFromRef"
      >
        <!-- 1.给el-form动态绑定model和rules属性， -->

        <!-- 定义要校验的字段 prop="username" -->
        <el-form-item prop="username">
          <el-input
            placeholder="请输入账号"
            prefix-icon="el-icon-user-solid"
            v-model.trim="loginFrom.username"
            ref="username"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            v-model.trim="loginFrom.password"
            ref="password"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
          <el-link type="text" @click="goRegister">去注册</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginApi } from "@/api/login.js";
export default {
  name: "Register",
  data() {
    return {
      loginFrom: {
        username: "",
        password: "",
      },
      // 登录的表单验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            pattern: /^[a-z\d]{2,10}$/i,
            message: "用户名必须是2-10的字母数字",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "密码必须是6-15的非空字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    // 注册跳转
    goRegister() {
      this.$router.push("/register");
    },

    handleLogin() {
      this.$refs.loginFromRef.validate(async (valid) => {
        if (!valid) return;
        // this.$message.success("登录成功");
        const {
          data: { code, message, token },
          // token 需要用vuex管理
        } = await loginApi(this.loginFrom);
        if (code === 0) {
          this.$message.success(message);
          this.$store.commit("user/setToken", token);
          this.$router.replace("/");
          // replace 不可以回退
          // push     可以回退
        } else {
          this.$message.warning(message);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.register-container {
  background: url("~@/assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;

  .register-box {
    width: 400px;
    height: 405px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url("~@/assets/images/login_title.png") center no-repeat;
    }
  }
}
.register-container {
  background: url("~@/assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;

  .register-box {
    width: 400px;
    height: 405px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      background: url("~@/assets/images/login_title.png") center no-repeat;
    }

    .el-button {
      width: 100%;
    }
  }
}
</style>